$(function (r) {
    init_conv();
})

function init_conv(){
    $.post(ctx+"conversation/loadMyNotice",{},function (r) {
        if(r.code===0){
            var data = r.data;
            $("#noticeBody").empty();
            if(data!=""){
                loadAllNotice(data);
            }
        }
    });
}

function loadAllNotice(data) {
    var div = "";
    var div1 = "";
    $.each(data,function (index,val) {
        var ctype = val.cConvType;
        if(ctype!="3"){
            var cont = val.cConvCont;
            var json = $.parseJSON(cont);
            var content = json.content;
            var namea = json.customername;
            var namess = namea.split(",");
            var name = "";
            if(namess.length>3){
                name = namea.substring(0,10)+"...";
            }else{
                name = namea;
            }
            var creatname = val.cConvCreatName;
            var times = val.cConvTime;
            var time = times.substring(0,times.lastIndexOf("."));
            $("#noticeTime").val(time);
            var title = val.cConvTitle;
            var cusid = json.customerid;
            var type = json.ntype;
            var shencont = json.shencont;
            var session = val.cConvSession;
            var creatid = val.cConvCreator;
            if(type=="2"){
                div = "<div style=\"margin-top:40px;min-height:110px;max-height:250px;\">\n" +
                    "                    <div class=\"text-center m-t-13\">"+time+"</div>\n" +
                    "                    <div class='col-md-3 col-sm-3 col-xs-3 p-l-0'></div>\n" +
                    "                    <div class='col-md-6 col-sm-6 col-xs-6 p-l-0 m-t-5'>\n" +
                    "                        <div class='pull-right p-l-0 p-r-0 col-md-12 col-sm-12 col-xs-12'\n" +
                    "                             style='min-height:110px;max-height:250px;border:solid 0.5px #d9d9d9;box-shadow:2px 2px 8px #888888;border-radius:2px;'>\n" +
                    "                            <div>\n" +
                    "                                <div class='col-md-12 col-sm-12 col-xs-12'><h4><p class='pull-left m-b-10'>"+title+"<p></h4></div>\n" +
                    "                                <div class='col-md-12 col-sm-12 col-xs-12 pull-left border-bottom-1'><h5><span class='p-l-15 p-t-3 m-5 color-blue'>"+creatname+" </span>"+content+"</h5><p class='p-l-20'>附加信息："+shencont+"</p></div>\n" +
                    "                                <button class=\"btn btn-xs btn-info m-5 m-l-35\" cusid='"+cusid+"' cid='"+creatid+"' sid='"+session+"' data='1' onclick='agreesShen(this)' type=\"button\">同意</button>\n" +
                    "                                <button class=\"btn btn-xs btn-danger m-5\" cusid='"+cusid+"' cid='"+creatid+"' sid='"+session+"' data='2' onclick='agreesShen(this)' type=\"button\">拒绝</button>\n" +
                    //"                                <button class=\"btn btn-xs btn-link m-5 pull-right\" type=\"button\">查看详情</button>\n" +
                    "                            </div>\n" +
                    "                        </div>\n" +
                    "                    </div>\n" +
                    "                </div>\n" +
                    "                <div class=\"clearfix\"></div>";
            }else if(type=="3"){
                div = "<div style=\"margin-top:40px;min-height:110px;max-height:250px;\">\n" +
                    "                    <div class=\"text-center m-t-13\">"+time+"</div>\n" +
                    "                    <div class='col-md-3 col-sm-3 col-xs-3 p-l-0'></div>\n" +
                    "                    <div class='col-md-6 col-sm-6 col-xs-6 p-l-0 m-t-5'>\n" +
                    "                        <div class='pull-right p-l-0 p-r-0 col-md-12 col-sm-12 col-xs-12'\n" +
                    "                             style='min-height:110px;max-height:250px;border:solid 0.5px #d9d9d9;box-shadow:2px 2px 8px #888888;border-radius:2px;'>\n" +
                    "                            <div>\n" +
                    "                                <div class='col-md-12 col-sm-12 col-xs-12'><h4><p class='pull-left m-b-10'>"+title+"<p></h4></div>\n" +
                    "                                <div class='col-md-12 col-sm-12 col-xs-12 pull-left border-bottom-1'><h5><span class='p-l-15 p-t-3 m-5 color-blue'>"+creatname+" </span>"+content+"</h5><p class='p-l-20'>附加信息："+shencont+"</p></div>\n" +
                    "                                <label class='m-5 p-2 p-l-1 m-l-35' style='color: #242a3070;'>已同意</label>"+
                    "                            </div>\n" +
                    "                        </div>\n" +
                    "                    </div>\n" +
                    "                </div>\n" +
                    "                <div class=\"clearfix\"></div>";
            }else if(type=="4"){
                div = "<div style=\"margin-top:40px;min-height:110px;max-height:250px;\">\n" +
                    "                    <div class=\"text-center m-t-13\">"+time+"</div>\n" +
                    "                    <div class='col-md-3 col-sm-3 col-xs-3 p-l-0'></div>\n" +
                    "                    <div class='col-md-6 col-sm-6 col-xs-6 p-l-0 m-t-5'>\n" +
                    "                        <div class='pull-right p-l-0 p-r-0 col-md-12 col-sm-12 col-xs-12'\n" +
                    "                             style='min-height:110px;max-height:250px;border:solid 0.5px #d9d9d9;box-shadow:2px 2px 8px #888888;border-radius:2px;'>\n" +
                    "                            <div>\n" +
                    "                                <div class='col-md-12 col-sm-12 col-xs-12'><h4><p class='pull-left m-b-10'>"+title+"<p></h4></div>\n" +
                    "                                <div class='col-md-12 col-sm-12 col-xs-12 pull-left border-bottom-1'><h5><span class='p-l-15 p-t-3 m-5 color-blue'>"+creatname+" </span>"+content+"</h5><p class='p-l-20'>附加信息："+shencont+"</p></div>\n" +
                    "                                <label class='m-5 p-2 p-l-1 m-l-35' style='color: #242a3070;'>已拒绝</label>"+
                    "                            </div>\n" +
                    "                        </div>\n" +
                    "                    </div>\n" +
                    "                </div>\n" +
                    "                <div class=\"clearfix\"></div>";
            }else if(type=="5"){
                div = "<div style=\"margin-top:40px;min-height:110px;max-height:250px;\">\n" +
                    "                    <div class=\"text-center m-t-13\">"+time+"</div>\n" +
                    "                    <div class='col-md-3 col-sm-3 col-xs-3 p-l-0'></div>\n" +
                    "                    <div class='col-md-6 col-sm-6 col-xs-6 p-l-0 m-t-5'>\n" +
                    "                        <div class='pull-right p-l-0 p-r-0 col-md-12 col-sm-12 col-xs-12'\n" +
                    "                             style='min-height:110px;max-height:250px;border:solid 0.5px #d9d9d9;box-shadow:2px 2px 8px #888888;border-radius:2px;'>\n" +
                    "                            <div>\n" +
                    "                                <div class='col-md-12 col-sm-12 col-xs-12'><h4><p class='pull-left m-b-10'>"+title+"<p></h4></div>\n" +
                    "                                <div class='col-md-12 col-sm-12 col-xs-12 pull-left border-bottom-1'><h5><span class='p-l-15 p-t-3 m-5 color-blue'>"+creatname+" </span>"+content+"</h5></div>\n" +
                    "                                <button class=\"btn btn-xs btn-link m-5 pull-right\" session ='"+session+"' onclick='loadNoticeDetail(this)' type=\"button\">查看详情</button>\n" +
                    "                            </div>\n" +
                    "                        </div>\n" +
                    "                    </div>\n" +
                    "                </div>\n" +
                    "                <div class=\"clearfix\"></div>";
            }else if(type=="1"){
                div = "<div style=\"margin-top:40px;min-height:110px;max-height:250px;\">\n" +
                    "                    <div class=\"text-center m-t-13\">"+time+"</div>\n" +
                    "                    <div class='col-md-3 col-sm-3 col-xs-3 p-l-0'></div>\n" +
                    "                    <div class='col-md-6 col-sm-6 col-xs-6 p-l-0 m-t-5'>\n" +
                    "                        <div class='pull-right p-l-0 p-r-0 col-md-12 col-sm-12 col-xs-12'\n" +
                    "                             style='min-height:110px;max-height:250px;border:solid 0.5px #d9d9d9;box-shadow:2px 2px 8px #888888;border-radius:2px;'>\n" +
                    "                            <div>\n" +
                    "                                <div class='col-md-12 col-sm-12 col-xs-12'><h4><p class='pull-left m-b-10'>"+title+"<p></h4></div>\n" +
                    "                                <div class='col-md-12 col-sm-12 col-xs-12 pull-left border-bottom-1'><h5><span class='p-l-15 p-t-3 m-5 color-blue'>"+creatname+" </span>"+content+"：<span class=\"m-5\">"+name+" </span></h5></div>\n" +
                    "                                <button class=\"btn btn-xs btn-link m-5 pull-right\" session ='"+session+"' onclick='loadNoticeDetail(this)' type=\"button\">查看详情</button>\n" +
                    "                            </div>\n" +
                    "                        </div>\n" +
                    "                    </div>\n" +
                    "                </div>\n" +
                    "                <div class=\"clearfix\"></div>";
            }
        }else{
            var times = val.cConvTime;
            var time = times.substring(0,times.lastIndexOf("."));
            var title = val.cConvTitle.substring(0,7);
            var session = val.cConvSession;
            div1 = "<div style=\"margin-top:40px;min-height:110px;max-height:250px;\">\n" +
                "                    <div class=\"text-center m-t-13\">"+time+"</div>\n" +
                "                    <div class='col-md-3 col-sm-3 col-xs-3 p-l-0'></div>\n" +
                "                    <div class='col-md-6 col-sm-6 col-xs-6 p-l-0 m-t-5'>\n" +
                "                        <div class='pull-right p-l-0 p-r-0 col-md-12 col-sm-12 col-xs-12'\n" +
                "                             style='min-height:110px;max-height:250px;border:solid 0.5px #d9d9d9;box-shadow:2px 2px 8px #888888;border-radius:2px;'>\n" +
                "                            <div>\n" +
                "                                <div class='col-md-12 col-sm-12 col-xs-12'><h4><p class='pull-left m-b-10'>月度数据报告<p></h4></div>\n" +
                "                                <div class='col-md-12 col-sm-12 col-xs-12 pull-left border-bottom-1'><h5><span class=\"m-5 \">"+title+"月分数据新鲜出炉</span></h5></div>\n" +
                "                                <button class=\"btn btn-xs btn-link m-5 pull-right\" uuid='"+session+"' onclick='loadRecord(this)' type=\"button\">查看详情</button>\n" +
                "                            </div>\n" +
                "                        </div>\n" +
                "                    </div>\n" +
                "                </div>\n" +
                "                <div class=\"clearfix\"></div>";
            $("#yuebaoBody").append(div1);
        }
        $("#noticeBody").append(div);
    });
}

/**同意共享*/
function agreesShen(btn){
    var uuid = $(btn).attr("cusid");
    var data = $(btn).attr('data');
    var session = $(btn).attr("sid");
    var cid = $(btn).attr("cid")
    if(uuid==''||data==''||uuid==undefined||data==undefined){$JuXue.n_danger("获取数据失败，请联系网站管理员。");return;}
    var url = ctx + "conversation/updateShare";
    $.post(url,{"uuid":uuid,"data":data,"session":session,"cid":cid},function (r) {
        if(r.code===0){
            init_conv();
            $JuXue.n_success("操作成功。")
        }else{
            $JuXue.n_danger("操作失败，请联系网站管理员。")
        }
    });
}

/**选择通知类型*/
function chooseType(){
    var type = $("#chooseNTypeId").val();
    var url = ctx + "conversation/chooseNoticeType";
    if(type=="0"){
        init_conv();
    }else{
        $.post(url,{"type":type},function (r) {
            if(r.code===0){
                var data = r.data;
                $("#noticeBody").empty();
                loadAllNotice(data);
            }
        })
    }
}

/**加载月报详情*/
function loadRecord(btn) {
    var session = $(btn).attr("uuid");
    $.post(ctx+"conversation/loadMonthRecordDetail",{},function (r) {
        $("#recordIdDetail").val(session);
        $("#monthRecordModal").empty().append(r).modal('show').draggable();
    });
}

function loadCRM() {
    $("#noticeContent").show();
    $("#yuebaoContent").hide();
}

function loadSys() {
    $("#noticeContent").hide();
    $("#yuebaoContent").show();
}

function loadNoticeDetail(btn) {
    var session = $(btn).attr("session");
    $.post(ctx+"conversation/loadNoticeDetail",{},function (r) {
        $("#noticeIdDetail").val(session);
        $("#noticeDetailModal").empty().append(r).modal('show').draggable();
    });
}